plotly es una poderosa librería que permite crear gráficos interactivos y visualmente atractivos directamente desde R. A diferencia de los gráficos estáticos generados con ggplot2, los gráficos de plotly permiten al usuario explorar los datos dinámicamente: hacer zoom, mover el gráfico, ver valores al pasar el cursor y seleccionar elementos específicos. (instructor: David Murillo).
library(plotly)
library(tidyverse)
library(ggplot2)
library(GGally)
library(FactoMineR)
library(factoextra)
Estudiantes = read.csv("data/StudentsPerformance.csv")
head(Estudiantes)
## gender race.ethnicity parental.level.of.education lunch
## 1 female group B bachelor's degree standard
## 2 female group C some college standard
## 3 female group B master's degree standard
## 4 male group A associate's degree free/reduced
## 5 male group C some college standard
## 6 female group B associate's degree standard
## test.preparation.course math.score reading.score writing.score
## 1 none 72 72 74
## 2 completed 69 90 88
## 3 none 90 95 93
## 4 none 47 57 44
## 5 none 76 78 75
## 6 none 71 83 78
plot_ly(data = Estudiantes, x = ~ math.score, y = ~ reading.score,
type = "scatter", mode = "markers",
color = ~ gender,
text = ~ paste("Puntuación de escritura:", writing.score),
marker = list(size =7)) |>
layout(title = "Calificación por género")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
Primero hacer una tabla resumen.
pre_conteos = Estudiantes |>
count(test.preparation.course) |>
rename("Cantidad" = n,
"Preparacion" = test.preparation.course)
Luego graficar.
plot_ly(data = pre_conteos, x = ~ Preparacion, y = ~ Cantidad,
type = "bar") |>
layout(title = "Estudiantes que se prepararon",
xaxis = list(title = "Preparación"),
yaxis = list(title = "Cantidad de estudiantes"))
plot_ly(data = Estudiantes, x = ~ math.score,
type = "histogram", nbinsx = 50,
marker = list(color = "lightblue"))
Primero hacer un ggplot (gráfico estático).
plot_math = ggplot(Estudiantes, aes(x = math.score, fill = gender)) +
geom_density() +
theme_classic()
plot_math
Luego agregar un plotly.
ggplotly(plot_math)
plot_ly(data = Estudiantes, x = ~ math.score, y = ~ lunch,
type = "box", boxpoints = "all",
color = ~ gender,
pointpos = -1.8,
jitter = 1.4) |>
layout(boxmode = "group")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
library(GGally)
ggpairs(Estudiantes[ ,6:8]) +
theme_classic() +
labs(title = "Correlación múltiple")
Luego crearlo como objeto.
CorPlot = ggpairs(Estudiantes[ ,6:8]) +
theme_classic() +
labs(title = "Correlación múltiple")
ggplotly(CorPlot)
## Warning: Can only have one: highlight
## Warning: Can only have one: highlight
library(FactoMineR)
library(factoextra)
Primero buscar una variables que sea factor (género), ó transformarla a factor.
PCA_genero = PCA(Estudiantes[ ,6:8])
Genero = as.factor(Estudiantes$gender)
fviz_pca_biplot(PCA_genero,
axes = c(1,2),
geom = c("point"),
palette = c("red", "blue"),
addEllipses = TRUE,
label = "none",
habillage = Genero,
col.var = NA) +
labs(title = "PCA Género",
y = "Dimensión 2(7.9%)",
x = "Dimensión 1(90.6%)",
color = "Género",
fill = "Género",
shape = "Género")
Luego convertirlo en objeto para pasarlo a plotly.
PCA_plot = fviz_pca_biplot(PCA_genero,
axes = c(1,2),
geom = c("point"),
palette = c("red", "blue"),
addEllipses = TRUE,
label = "none",
habillage = Genero,
col.var = NA) +
labs(title = "PCA Género",
y = "Dimensión 2(7.9%)",
x = "Dimensión 1(90.6%)",
color = "Género",
fill = "Género",
shape = "Género")
ggplotly(PCA_plot)